﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MvcContrib.Pagination;

using ENail.Domain.Core;
using Equation.Enterprise.Framework.Domain.Model;
using Equation.Enterprise.Framework.Web.Model;
using Equation.Enterprise.Utilities.DesignByContract;
using ENail.Domain.Core.Contract.Services;


namespace ENail.Portal.Web.Models
{
    public class ReceiptViewModel :
        BaseViewModel<int>
    {
        public int RegistrationId { get; set; }
        public string CustomerName { get; set; }
        public DateTime StartTime { get; set; }
        public IList<ReceiptItemListViewItem> ReceiptItems { get; set; }
        public double SubTotal { get; set; }
        public double Discount { get; set; }
        public double Tax { get; set; }
        public double Total { get; set; }

        public ReceiptViewModel()
        {
            ReceiptItems = new List<ReceiptItemListViewItem>();
        }
    }

    public class ReceiptItemListViewItem :
        BaseViewModel<int>
    {
        public int ReceiptId { get; set; }
        public int NailServiceId { get; set; }
        public string NailServiceName { get; set; }
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public double Price { get; set; }
        public bool IsSelected { get; set; }
    }

    public class ReceiptViewModelMapper : ViewModelMapper<ReceiptViewModel, Receipt>
    {
        public ReceiptViewModelMapper()
        {
        }

        public override void MapDomainToView(Receipt domainModel, ReceiptViewModel viewModel)
        {
            Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.RegistrationId = domainModel.Registration.Id;
            viewModel.CustomerName = domainModel.Registration.Customer.FirstName + ", " + domainModel.Registration.Customer.LastName;
            viewModel.StartTime = domainModel.Registration.StartTime;
            viewModel.Id = domainModel.Id;
            viewModel.SubTotal = domainModel.SubTotal;
            viewModel.Discount = domainModel.Discount;
            viewModel.Tax = domainModel.Tax;
            viewModel.Total = domainModel.Total;

            mapDomainItemsToView(domainModel, viewModel);
        }

        public override void MapViewToDomain(ReceiptViewModel viewModel, Receipt domainModel)
        {
            Check.Require(viewModel != null);
            Check.Require(domainModel != null);

            domainModel.Discount = viewModel.Discount;
            domainModel.Tax = viewModel.Tax / 100;
        }

        private void mapDomainItemsToView(Receipt domainModel, ReceiptViewModel viewModel)
        {
            Check.Require(domainModel != null);
            Check.Require(viewModel != null);

            viewModel.ReceiptItems = (from e in domainModel.ReceiptItems
                                     select new ReceiptItemListViewItem
                                     {
                                        Id = e.Id,
                                         ReceiptId = e.Receipt.Id,
                                         NailServiceId = e.Assignment.NailService.Id,
                                         NailServiceName = e.Assignment.NailService.ServiceName,
                                         EmployeeId = e.Assignment.Employee.Id,
                                         EmployeeName = e.Assignment.Employee.FirstName + ", " + e.Assignment.Employee.LastName,
                                         Price = e.Price,
                                         IsSelected = false
                                     })
                                    .OrderBy(x => x.Id).ToList();
        }
    }
}